package com.atguigu.filter;

import com.atguigu.utils.JdbcUtils;

import javax.servlet.*;
import java.io.IOException;

public class TransactionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }
    /*数据库事务优化*/
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            filterChain.doFilter(servletRequest,servletResponse);
            JdbcUtils.commitAndCloseConnection();//提交事务，关闭连接
        } catch (Exception e) {
            JdbcUtils.rollbackAndCloseConnection();//回滚事务，关闭连接
            e.printStackTrace();
            throw new RuntimeException(e);
            //把异常抛给Tomcat管理展示友好的错误页面
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
